import { Component, OnInit, ViewChild } from '@angular/core';
import { NotificationService } from "../../shared/utils/notification.service";
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
  selector: 'sa-make-test',
  templateUrl: './make-test.component.html'
})
export class MakeTestComponent implements OnInit {
  tableData: any[] = []; //table数据
  tabletotalCount: number = 0; //分页总条数
  curPage: number = 1; //分页当前页
  pageSize: number = 10; //内置的分页每页条数

  startTime: string = ''
  endTime: string = ''

  constructor(private jsonApiService: JsonApiService, private notificationService: NotificationService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
    });
  }
  carDeatailUseList = carTypeListObj.carDeatailUseList

  ngOnInit() {
    this.initDate().then(() => {
      this.load();
    })
  }
  //初始化时间
  initDate() {
    var that = this
    return new Promise(function (resolve, reject) {
      System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(() => {
        System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
          $('#datetimepicker1')
            .datetimepicker({
              //时间插件
              format: 'yyyy-mm',
              language: 'zh-CN',
              weekStart: 1,
              autoclose: 1,
              startView: 'year',
              maxView: 'decade',
              minView: 'year',
              forceParse: false,
              pickerPosition: 'bottom-left'
            })
            .on('changeDate', function (e) {
              const date = e.date
              const year = date.getFullYear()
              const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
              that.startTime = `${year}-${month}`
              $('#datetimepicker2').datetimepicker('setStartDate', that.startTime)
            })
          $('#datetimepicker2')
            .datetimepicker({
              //时间插件
              format: 'yyyy-mm',
              language: 'zh-CN',
              weekStart: 1,
              autoclose: 1,
              startView: 'year',
              maxView: 'decade',
              minView: 'year',
              forceParse: false,
              pickerPosition: 'bottom-left'
            })
            .on('changeDate', function (e) {
              const date = e.date
              const year = date.getFullYear()
              const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
              that.endTime = `${year}-${month}`
              $('#datetimepicker1').datetimepicker('setEndDate', that.endTime)
            })
          resolve(null)
        })
      })
    })
  }

  //table刷新
  refresh() {
    this.load();
  }
  //分页回调
  tablePaginate(e) {
    this.curPage = e.page + 1;
    this.pageSize = e.rows;
    this.load();
  }

  //表格搜索
  table_search() {
    this.curPage = 1;
    this.load();
  }
  //初始化加载list数据
  load() {
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      isExport: false,
      parameters: {
        startTime: this.startTime,
        endTime: this.endTime
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data);
    this.jsonApiService.http("checkReport/checkReportPageList.html", reqdata).subscribe(data => {
      if (data.isSuccess) {
        this.tableData = JSON.parse(JSON.stringify(data.exhibitDatas));
        for(let i=0;i <data.exhibitDatas.length;i++){
          const date = data.exhibitDatas[i].checkDate.split('-')
          const dateValue = date[0] + '年' + date[1] +'月'
          this.tableData[i].checkDate = dateValue
        }
        this.tabletotalCount = data.recordCount;
      } else {
        this.jsonApiService.message(data.message);
      }
    })
  }

  // 数据导出
  exportData() {
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      isExport: true,
      parameters: {
        startTime: this.startTime,
        endTime: this.endTime
      }
    }
    common.exprotDataFile(data, 'gridPager', config.BaseURL + 'checkReport/checkReportPageList.html')
  }

  synchro() {
    //同步必须先选择区间区间
    if (!this.startTime || !this.endTime) {
      this.jsonApiService.message('请先选择时间区间再进行同步');
      return
    }
    let data = {
      startTime: this.startTime + '-01 00:00:00',
      endTime: this.endTime + '-01 00:00:00'
    }
    this.jsonApiService.http_obj("checkReport/checkReportSync.html", data).subscribe(data => {
      this.jsonApiService.message(data.message);
      if (data.success) {
        this.pageSize = 10;
        this.curPage = 1;
        this.load();
      }
    })
  }

  //删除所选时间
  del_date() {
    this.startTime = ''
    this.endTime = ''
    $('#datetimepicker1').val('').change()
    $('#datetimepicker2').val('').change()
    $('#datetimepicker2').datetimepicker('setStartDate', null)
    $('#datetimepicker1').datetimepicker('setEndDate', null)
  }


  /**
   * 查看
   */
  typeName: string = ''
  viewType: string | number
  viewCarData: any = []
  viewDataObj: any = {
    orderNum: '',
    testingNum: '',
    badNum: '',
    badrate: '',
    id: '',
  }
  //查看车辆列表
  viewRow(row, modal, type) {
    this.viewDataObj.viewId = row.id
    this.viewDataObj.orderNum = row.orderCode;
    this.viewType = type
    if (type == 1) {
      this.viewDataObj.testingNum = row.smtCheckNum;
      this.viewDataObj.badNum = row.smtBadNum;
      this.viewDataObj.badrate = row.smtBadRatio
    } else if (type == 2) {
      this.viewDataObj.testingNum = row.pcbaCheckNum;
      this.viewDataObj.badNum = row.pcbaBadNum;
      this.viewDataObj.badrate = row.pcbaBadRatio
    } else if (type == 3) {
      this.viewDataObj.testingNum = row.hostCheckNum;
      this.viewDataObj.badNum = row.hostBadNum;
      this.viewDataObj.badrate = row.hostBadRatio
    }


    if (type == 1) {
      this.typeName = 'SMT检测不良原因'
    } else if (type == 2) {
      this.typeName = 'PCBA烧录测试不良原因'
    } else {
      this.typeName = '主机组装测试不良原因'
    }
    modal.show()
    this.requestCarList();
  }
  closeViewCarModal(modal) {
    modal.hide();
  }
  //请求车辆列表
  requestCarList() {
    this.jsonApiService.http_get("checkReport/queryBadInfo.html?id=" + this.viewDataObj.viewId + '&type=' + this.viewType).subscribe(data => {
      if (data.success) {
        this.viewCarData = data.data
      } else {
        this.jsonApiService.message(data.message);
      }
    })
  }
}